package controller;

import java.util.List;

import model.User;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import dao.IUserDAO;
import factory.Factory;

public class HibernateController
{
    private static SessionFactory sessionFactory = null;

    static
    {
        try
        {
            //creates the session factory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

    public static void main(String[] args)
    {
        try
        {
            User s1 = new User();
            User s2 = new User();

            s1.setName("Ivanov Ivan");
            s1.setAge(21);
            s2.setName("Petrova Alisa");
            s2.setAge(24);

            IUserDAO userDAO = Factory.getInstance().getUserDAO();

            userDAO.adduser(s1);
            userDAO.adduser(s2);

            List<User> studs = userDAO.getAllUsers();
            System.out.println("======== All users =========");
            for (int i = 0; i < studs.size(); ++i)
            {
                User user = studs.get(i);
                System.out.println("User name  : " + user.getName() + ", Age : " + user.getAge() + ",  id : " + user.getId());
                System.out.println("=============================");
            }
        }
        catch (Throwable e)
        {
            e.printStackTrace();
        }
    }
}
